﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AgencyEntityLib.BaseEntity;

using Ecopsy.Tools;
using OutdoorLib.BaseEntity.WS;
using OutdoorLib.BaseEntity.Agency;
using OutdoorLib.BaseEntity.Dictionary;
using Grishko.DataTier;
using Grishko.UI;

public partial class iFarmeDictionary_TreeDictonary : System.Web.UI.Page, ISiteMapItem 
{
    #region Private
    private wsDictionary _ws_Dictionary = (wsDictionary)GlobalConstants.CurrentAgency.WS_FactoryList[WS_FactoryList.wsKey_Dictionary];

    private string TreeDictionaryID { get { return Request.QueryString["TreeDictionaryID"]; } }

    private string FolderID { get { return Request.QueryString["FolderID"]; } }


    protected bool IsSystemDictionry
    {
        get { return true; }
        //get { return Dictionary.Get_TreeDictionaryTypeList_Item().IsSystem > 0; } 
    }

    private TreeDictionaryItem Dictionary
    {
        get
        {
            return null;
            ////поднять службу справочников
            //wsDictionary _ws_Dictionary = (wsDictionary)GlobalConstants.CurrentAgency.WS_FactoryList[WS_FactoryList.wsKey_Dictionary]; ;
            //// получить справочник 
            //TreeDictionaryItem tree = (TreeDictionaryItem)_ws_Dictionary.GetTreeDictionary(FolderID, TreeDictionaryID);
            
            //if (null == tree) 
            //        tree = _ws_Dictionary.CreateTreeDictionary(FolderID, TreeDictionaryID, "Новый справочник");            
            //return tree;
        }
    }

    ///// <summary>
    ///// вернуть требуемый вид справочника
    ///// </summary>
    ///// <returns></returns>
    //private F_TreeDataSource GetTreeSource()
    //{
    //    return null;        
    //}
   
    
    #endregion
    
    #region Protected
    protected void Page_Load(object sender, EventArgs e)
    {
        tvDictionaryTree.OnSelectedNodeChanged = OnNodeChange;
        tvDictionaryTree.OnAddNode = OnAddChange;
        tvDictionaryTree.OnRemoveNode = OnRemoveChange;
        tvDictionaryTree.GetCallContextMenuWS = GetCallContextMenuWS;
        
        if (!IsPostBack)
        {
            MyBind();
        }
    }

    private string GetCallContextMenuWS(string NodeId)
    {
        Grishko.UI.TreeContextMenu.MenuContextValue mv = new TreeContextMenu.MenuContextValue() 
                                                            { DictionaryID = TreeDictionaryID, 
                                                              FolderID = FolderID, 
                                                              NodeId = NodeId,
                                                              ClientAddClick = "CallAddNodeEvent(\\'" + tvDictionaryTree.hf_EventArgID + "\\',\\'" + TreePostbackControl.ContextMenu_AddEventPref + NodeId + " \\');",
                                                              ClientRemoveClick = "CallRemoveNodeEvent(\\'" + tvDictionaryTree.hf_EventArgID + "\\', \\'" + TreePostbackControl.ContextMenu_RemoveEventPref + NodeId + " \\');",
                                                            };                 
        return "CallContextMenu(event,"+mv.ToString () +" ) ;";
    }


    private bool OnAddChange(string NodeID)
    {
        TreeDictionaryItem tr = (TreeDictionaryItem)Dictionary.Find(NodeID);
        TreeDictionaryItem i = new TreeDictionaryItem() { Value  = "Новое значение" };
        tr.ChildNodes.Add (i);
        txtNodeVal.Text = i.Value.ToString() ; 
        tvDictionaryTree.SelectedNodeValue = i.ID;  
        MyBind();

        return true;
    }

    private bool OnRemoveChange(string NodeID)
    {
        if (SelectedValue.Equals(string.Empty)) return true;
        //_ws_Dictionary.RemoveTreeDictionaryBranch(FolderID, TreeDictionaryID, SelectedValue);
        MyBind();
        return true;
        
    }
    private bool  OnNodeChange(string NodeID)
    {
        string s = NodeID;
        TreeDictionaryItem  tr =(TreeDictionaryItem  ) Dictionary.Find(s);
        //txtNodeVal.Text = tr.Name;
        MyBind();
        return true;
    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {

    }
   

    protected string SelectedValue
    {
        get
        {


            if (null == tvDictionaryTree.SelectedNodeValue) return string.Empty;
            return tvDictionaryTree.SelectedNodeValue;

        }
    }

    ///// <summary>
    ///// Не трогать если  0 уровень, если не выделен parent
    ///// </summary>
    //protected bool EnableDel 
    //{
    //    get 
    //    {
    //        //if (null == tvDictionary.SelectedNode) return false ;
    //        //if (0 == tvDictionary.SelectedNode.Depth) return false;
    //        return true;
    //    }
    //}

    //protected bool EnableAdd
    //{
    //    get
    //    {
    //        //if (null == tvDictionary.SelectedNode) return false;            
    //        return true;
    //    }
    //}
   
    private void MyBind()
    {
        tvDictionaryTree.DataSource = Dictionary;
        tvDictionaryTree.DataBind();

        
        //tvDictionary.DataSource = GetTreeSource();
        //DataBind();

    }
    ///// <summary>
    ///// Формирование узла дерева
    ///// </summary>
    ///// <param name="tree"></param>
    ///// <param name="nd"></param>
    ///// <returns></returns>
    //private Control  DictionaryTree_BuildNode(TreeControl tree,  TreeEntitySingle nd)
    //{
    //    //Button btn = new Button();
    //    //btn.CssClass = DictionaryTree.CssNode;
    //    //btn.Text = (nd as TreeDictionaryItem).Name;
    //    //btn.ID = "ctl00$cphBody$Button1";
    //    //btn.Click += new EventHandler(Node_Click);
    //    //return btn;


    //    //LinkButton btn = new LinkButton();
    //    //btn.CssClass = DictionaryTree.CssNode;
    //    //btn.Text = (nd as TreeDictionaryItem).Name;
    //    //btn.OnClientClick =" MyPostBack('buttoonsXXX', 'arg')";
    //    return null;
    //}

    protected void Node_Click(object sender, EventArgs e)
    {
        Button btn = (Button)sender;
        txtNodeVal.Text = btn.Text;
    }
    

    protected void tvDictionary_SelectedNodeChanged1(object sender, EventArgs e)
    {
        DataBind();
    }
       
    /// <summary>
    /// Если есть выделенная ветка - считать в нее значение txtNodeVal.Text
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (SelectedValue.Equals(string.Empty))
        {
            MyBind();
            return;
        }

        //_ws_Dictionary.UpdateTreeDictionaryBranch(FolderID, TreeDictionaryID, SelectedValue, txtNodeVal.Text);
        MyBind ();
        wsAgency.Reset ();        
    }
    
    protected void AddButton_Click(object sender, EventArgs e)
    {
        if (SelectedValue.Equals(string.Empty)) return;
        //_ws_Dictionary.AddTreeDictionaryBranch(FolderID, TreeDictionaryID, SelectedValue, "Новое значение");
        MyBind();
    }

    
#endregion




    #region ISiteMapItem Members

    public string SiteMapKey
    {
        get { throw new NotImplementedException(); }
    }

    #endregion
  
}
